#if run
#include <windows.h>
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x502
#include <stdio.h>
#endif

#include "resource.h"
#include "checkpage.h"
#include "init.h"


HINSTANCE hInstance;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) {
//////////////////////////////////////////////////////////////
  WNDCLASS wnd;
  wnd.style = CS_HREDRAW | CS_VREDRAW; //we will explain this later
  wnd.lpfnWndProc = WndProc;
  wnd.cbClsExtra = 0;
  wnd.cbWndExtra = 0;
  wnd.hInstance = hInstance;
  wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); //default icon
  wnd.hCursor = LoadCursor(NULL, IDC_ARROW);   //default arrow mouse cursor
  wnd.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  wnd.lpszMenuName = NULL;                     //no menu
  wnd.lpszClassName = TEXT("HTTPSiteCheck");
  if(!RegisterClass(&wnd)) {
    MessageBox(NULL, "This Program Requires Windows NT", "Error", MB_OK);
    return 0;
  }
///////////////////////////////////////////////////////////////
  hInstance = hInst;
  MSG msg;
  HWND hwnd;

  RECT rc;
  GetWindowRect(GetDesktopWindow(), &rc);

  hwnd = CreateWindowEx(WS_EX_DLGMODALFRAME ,
                        TEXT("HTTPSiteCheck"),
                        TEXT("HTTP Site Check"),
                        WS_VISIBLE | WS_SYSMENU | WS_CAPTION ,
                        (rc.right-400)/2,
                        (rc.bottom-300)/2,
                        400,
                        300,
                        NULL,
                        NULL,
                        hInstance,
                        NULL);
  ShowWindow(hwnd, iCmdShow);              //display the window on the screen
///////////////////////////////////////////////////////////////
  init(hwnd);
///////////////////////////////////////////////////////////////
  while(GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  switch(msg) {
    case WM_COMMAND: {
      if ((DWORD)lParam == (DWORD)langlist->Wnd) {
        if (CBN_SELCHANGE == HIWORD(wParam)) {
          delete language->setCurrentByLabel(langlist->getCurText());
          controls->setLanguage(language);
        }
      } else if ((HWND)lParam == pages->getCloseButtonHwnd()) {
        SendMessage(hwnd,WM_DESTROY,0,0);
      } else if ((HWND)lParam == pages->getNextButtonHwnd()) {
        pages->nextPage();
        if (pages->getPage() == 2) {
          CheckPage(hwnd,0);
        }
      } else if ((HWND)lParam == pages->getPrevButtonHwnd()) {
        pages->prevPage();
      }
      return 0;
    }
    case WM_TIMER:{
      switch (wParam) {
        case TIMER_START: {
          KillTimer(hwnd,TIMER_START);
          edit->setText(data->getHttp());
          return 0;
        }
        case TIMER_STEP1: {
          KillTimer(hwnd,TIMER_STEP1);
          CheckPage(hwnd,1);
          return 0;
        }
        case TIMER_STEP2: {
          KillTimer(hwnd,TIMER_STEP2);
          CheckPage(hwnd,2);
          return 0;
        }
        case TIMER_STEP3: {
          KillTimer(hwnd,TIMER_STEP3);
          CheckPage(hwnd,3);
          return 0;
        }
        case TIMER_STEP4: {
          KillTimer(hwnd,TIMER_STEP4);
          CheckPage(hwnd,4);
          return 0;
        }
      }
      return 0;
    }
    case WM_CREATE:{
      SendMessage(hwnd, (UINT)WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)LoadIcon(hInstance, MAKEINTRESOURCE(ICON_MAIN)));
      return 0;}
    case WM_DESTROY:
      data->setHTTP(edit->getText());
      data->checkSave();
      setup->checkSave();
      PostQuitMessage(0);
      return 0;
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}
